//
//  BeautyViewController.m
//  ALFashion
//
//  Created by dengzhicheng on 15/6/15.
//  Copyright (c) 2015年 dengzhicheng. All rights reserved.
//

#import "BeautyViewController.h"
#import "BeautyCell.h"
#import <AFNetworking.h>
#import "BeautyListModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "BeautyDeatilController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface BeautyViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation BeautyViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
    
    [self setNavigation];
    [self createSubView];
    [self starConnect];
    
    
    
    
}

//网络请求
- (void)starConnect
{
    NSString *url = @"http://www.cnsunpower.com/iosapi/API_NEWS_TITLE.php?page=1&num=60&categoryId=37";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSData *data = operation.responseData;
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [bigDic objectForKey:@"ArticleList"];
        for (NSDictionary *dic in array) {
            BeautyListModel *model = [[BeautyListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.array addObject:model];
        }
        [self createTable];

        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error = %@", error);
        
    }];
}




// 设置导航栏
- (void)setNavigation
{
    // 自定义view, 添加到导航栏的titleView上
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleView.frame];
    titleLabel.text = @"ALFashion";
    titleLabel.font = [UIFont fontWithName:@"huxiaobo-gdh" size:30];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:titleLabel];
    self.navigationItem.titleView = titleView;
    
    // 自定义button, 添加到导航栏的leftButtonItem上
    UIButton *menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 20, 20)];
    [menuButton setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
    [menuButton setImage:[UIImage imageNamed:@"menuSelected.png"] forState:UIControlStateHighlighted];
    [menuButton addTarget:self action:@selector(menuButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
}


// menu 按钮点击事件
- (void)menuButtonClicked:(UIButton *)button
{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MenuView" object:@(YES)];
    
}

- (void)createSubView
{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
    [label setBackgroundColor:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
    label.text = @"美容  BEAUTY";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    
}
- (void)createTable
{
    UITableView *table =[[UITableView alloc]initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT - 20 - 64) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.rowHeight = 0.35 * WIDTH;
    [self.view addSubview:table];
    
}
#pragma mark tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BeautyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[BeautyCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    
    BeautyListModel *model = [self.array objectAtIndex:indexPath.row];
    
    [cell.showImage sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"loading.gif"]];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:19]};
    CGRect rect = [model.title boundingRectWithSize:CGSizeMake(WIDTH - 0.402 * WIDTH - 0.026 * WIDTH, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    cell.title.frame = CGRectMake(0.352 *WIDTH, 0.024 * WIDTH, WIDTH - 0.35 * WIDTH - 0.026 * WIDTH, rect.size.height);
    cell.title.text = model.title;
    cell.subTitle.frame = CGRectMake(cell.title.frame.origin.x, cell.title.frame.origin.y + rect.size.height, cell.title.frame.size.width, 0.35 * WIDTH - cell.title.frame.origin.y - rect.size.height - 0.024 * WIDTH);
    cell.subTitle.text = model.subtitle;
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BeautyListModel *model = [self.array objectAtIndex:indexPath.row];
    BeautyDeatilController *detail = [[BeautyDeatilController alloc]init];
    detail.artid = model.artid;
    [self.navigationController pushViewController:detail animated:YES];
    
}
@end
